<?php

class Site_CommentController extends Fengqiaoju_Controller_Action {

	public function init() {

		parent::init ();
		$this->site_id = $this->site_id = $this->_request->getParam ( 'site_id' );
		$this->model_id = $this->_request->getParam ( 'model_id' );
		$this->item_id = $this->_request->getParam ( 'item_id' );
		$this->model_type = $this->_request->getParam ( 'model_type' );
		$this->commentObject = new Site_Models_Comment ();
		$this->_helper->layout->disableLayout ();
		$this->_helper->viewRenderer->setNoRender ();
	}

	public function indexAction() {

	}

	/**
	 * 添加评论
	 */
	public function addAction() {

		if (! $data ['site_id'] = $this->_request->getPost ( 'site_id' )) {
			die ( 'site_id 丢失' );
		}
		if (! $data ['user_id'] = $this->userObject->getUserId ()) {
			die ( '请登入' );
		}
		if (! $data ['model_type'] = $this->_request->getPost ( 'model_type' )) {
			die ( 'model_type 丢失' );
		}
		if (! $data ['item_id'] = $this->_request->getPost ( 'item_id' )) {
			die ( 'item_id 丢失' );
		}
		if (! $data ['model_id'] = $this->_request->getPost ( 'model_id' )) {
			die ( 'model_id 丢失' );
		}
		if (! $data ['comment_text'] = $this->_request->getPost ( 'comment_text' )) {
			die ( '请写评论' );
		}
		$data ['comment_inputtime'] = date ( 'Y-m-d H:i:s' );
		
		if ($this->commentObject->insert ( $data )) {
			die ( '提交成功' );
		}
	
	}

	/**
	 * 获取评论
	 */
	
	public function listAction() {

		$numPerPage = 10;
		$where = "model_type='" . $this->model_type . "'";
		if ($this->site_id) {
			$where .= " AND site_id=" . intval ( $this->site_id );
		}
		if ($this->item_id) {
			$where .= " AND item_id=" . intval ( $this->item_id );
		}
		if ($this->model_id) {
			$where .= " AND model_id=" . intval ( $this->model_id );
		}
		
		$comment = $this->commentObject->fetchAll ( $where, 'comment_inputtime DESC', $numPerPage, $_page );
		return $comment;
	}
}